package io.vertx.examples.spring.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.text.DecimalFormat;

/**
 * 响应结果生成工具
 */
public class ResultGenerator {

    private static final String STR_FORMAT = "0000000000";

    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";

    public static Result genSuccessResult() {
        return new Result()
                .setCode(ResultCode.SUCCESS)
                .setMessage(DEFAULT_SUCCESS_MESSAGE);
    }

    public static Result genSuccessResult(Object data) {
        DecimalFormat df = new DecimalFormat(STR_FORMAT);

        Result result = new Result()
                .setCode(ResultCode.SUCCESS)
                .setMessage(DEFAULT_SUCCESS_MESSAGE)
                .setData(data);
        Long len = Integer.valueOf(JSON.toJSONString(result, SerializerFeature.BrowserCompatible,SerializerFeature.WriteNonStringValueAsString).length()).longValue();
        result.setData_len(df.format(len));
        return result;
    }
    public static Result genSuccessResult(Object data,int code) {
        DecimalFormat df = new DecimalFormat(STR_FORMAT);

        Result result = new Result()
                .setCode(code)
                .setMessage(DEFAULT_SUCCESS_MESSAGE)
                .setData(data);
        Long len = Integer.valueOf(JSON.toJSONString(result, SerializerFeature.BrowserCompatible,SerializerFeature.WriteNonStringValueAsString).length()).longValue();
        result.setData_len(df.format(len));
        return result;
    }

    public static Result genSuccessResult(String message,int resultCode) {
        return new Result()
                .setCode(resultCode)
                .setMessage(message);
    }

    public static Result genSuccessResult(Object data,String message) {
        Result result = genSuccessResult(data);
        result.setMessage(message);
        return result;
    }

    public static Result genFailResult(String message) {
        return new Result()
                .setCode(ResultCode.FAIL)
                .setMessage(message);
    }

    public static Result genFailResult(String message,ResultCode resultCode) {
        return new Result()
                .setCode(resultCode)
                .setMessage(message);
    }
    public static Result genFailResult(String message,int resultCode) {
        return new Result()
                .setCode(resultCode)
                .setMessage(message);
    }

    public static Result genFailResultDebug() {
    	return new Result()
    			.setCode(ResultCode.FAIL)
    			.setMessage("非调试模式");
    }
}
